package com.boyaa.shells.tool 
{
	import com.boyaa.shells.control.GlobalVar;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	
	/**
	 * 照相
	 * @author fly
	 */
	public class Photograph 
	{
		private var whileChild_Parent_Arr:Array = null;			//	父子数组
		
		public function Photograph() 
		{
			
		}
		
		public function takePhotograph(_dis:DisplayObject,wight:int,height:int):BitmapData
		{
			
			whileChild_Parent_Arr = new Array();
			var bitmapData:BitmapData = new BitmapData(wight, height);
			whileChild(_dis as DisplayObjectContainer);
			
			bitmapData.draw(_dis);
			resumeWhileChild();
			return bitmapData;
		}
		
		private function whileChild(dis:DisplayObjectContainer):void
		{
			for (var i:int = 0 ; i < dis.numChildren ; i++ )
			{
				var loader:DisplayObject = dis.getChildAt(i) as DisplayObject;
				if (loader is Loader)
				{
					//var loader:Loader = dis.getChildAt(i) as Loader;
					whileChild_Parent_Arr.push([loader.parent , loader]);
					loader.parent.removeChild(loader);
					i--;
					continue;
				}
				if (loader is DisplayObjectContainer)
				{
					whileChild(dis.getChildAt(i) as DisplayObjectContainer);
				}	
			}
		}
		
		private function resumeWhileChild():void
		{
			for (var i:int = 0; i < whileChild_Parent_Arr.length ; i ++)
			{
				whileChild_Parent_Arr[i][0].addChild(whileChild_Parent_Arr[i][1]);
			}
			whileChild_Parent_Arr = [];
		}
		
	}

}